/**
 * Handles Agent Creation, and sends agents to the platform to be sent, also handles agents
 * to be received.
 * @author jon
 */

package manager;

import java.util.Vector;
import ui.IdleScreen;
import ap.NoSuchElementException;
import agent.*;

public class IOHandlerManager implements Runnable {
	private ap.AgentPlatform agentPlatform;
	private AgentManagerMIDlet midlet;
	private boolean isAgentWaiting;
	
	
	public IOHandlerManager(AgentManagerMIDlet midlet,String serverAddress, String mmsAddress){
		this.midlet = midlet;
		isAgentWaiting = false;
		serverAddress = serverAddress.substring(10);
		String UDPaddress = "";
		int UDPaddress2;
		UDPaddress2 = 10 + Integer.parseInt(serverAddress);
		UDPaddress += UDPaddress2;
		
		System.err.println("Creating AP, UDP Address is: " + UDPaddress + " TCP Address is: " + serverAddress);
		agentPlatform = new ap.AgentPlatform(this, UDPaddress, serverAddress,  mmsAddress);
	}
	
	public boolean isAgentWaiting(){
		return isAgentWaiting;
	}
	
	public void agentWaiting(){
		this.isAgentWaiting = true;
	}
	
	public void run() {
	}

	public Object[] vecToArray(Vector v){
		Object[] array = new String[v.size()];
		for(int i = 0 ; i < array.length ; i++){
			array[i] = v.elementAt(i);
		}
		return array;
	}
	
	public void createAgent(Vector dests, String type, Vector startTimes, Vector endTimes, String message){
		if(type.equals(agent.Agent.TYPE_MEETING)){
			//create meeting agent and send.
			int numDestinations = dests.size();
			int numTimes = startTimes.size();
			boolean[][] rTimes = new boolean[numDestinations][numTimes];
			String[] destinations = (String[])vecToArray(dests);
			String[] meetingStartTimes = (String[])vecToArray(startTimes);
			String[] meetingEndTimes = (String[])vecToArray(endTimes);

			MeetingAgent agent;
			agent = new MeetingAgent();

			for(int i = 0 ; i < destinations.length ; i++)
				agent.addContact("tempcontactname" + i, destinations[i]);
			
			for(int i = 0 ; i < meetingStartTimes.length ; i++)
				agent.addTime(meetingStartTimes[i], meetingEndTimes[i]);
			
			this.sendAgent(agent);
		}
		else if(type.equals(agent.Agent.TYPE_INFORMATION)){
			//create confirmation agent and send.
			agent.InfoAgent infoAgent = new agent.InfoAgent();
			String[] destinations = (String[])vecToArray(dests);
			
			for(int i = 0 ; i < destinations.length ; i++)
				infoAgent.addContact("tempcontactname" + i, destinations[i]);

			infoAgent.setMessage(message);
			this.sendAgent(infoAgent);
		}
		midlet.getUIHandlerManager().setIdleScreen();
	}
	
	public void sendAgent(Agent agent){
		agentPlatform.setOutAgent(agent);
	}
	
	/*Receive an agent via the agent Platform */
	public Agent receiveAgent(){
		Agent agent = null;
		try{
			agent = agentPlatform.getInAgent();
		}catch(NoSuchElementException e){
			System.err.println("No Such Element Exception thrown in IOHandler.receiveAgetn()");
			e.printStackTrace();
		}
		
		return agent;			
	}
}
